package com.huqing.icu.base;

import com.huqing.icu.enums.BusinessConstants;
import com.huqing.icu.exceptions.BusinessException;
import com.huqing.icu.utils.HeaderUtil;
import com.huqing.icu.utils.ProtocolUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.apache.commons.lang.StringUtils;

/**
 * 处理请求基类
 */
public class BaseController {

    /**
     * 根据增删改操作时影响的行数返回成功或失败
     */
    protected String toResultJson(int rows) {
        return rows > 0 ? jsonSuccess(1) : jsonFail();
    }

    /**
     * json格式成功返回
     *
     * @param <T>
     * @param data
     * @param message
     */
    protected <T> String jsonSuccess(T data, String message) {
        return renderJson(data, message);
    }

    /**
     * json格式成功返回
     *
     * @param <T>
     * @param data
     * @param message
     */
    protected <T> String jsonSuccessForNull(T data, String message) {
        return renderJsonForNull(data, message);
    }

    /**
     * json格式成功返回
     *
     * @param <T>
     * @param data
     * @return
     */
    protected <T> String jsonSuccess(T data) {
        return renderJson(data);
    }

    /**
     * json格式成功返回
     *
     * @param <T>
     * @param data
     * @return
     */
    protected <T> String jsonSuccessForNull(T data) {
        return renderJsonForNull(data, null);
    }

    /**
     * json格式成功返回
     *
     * @param <T>
     * @return
     */
    protected <T> String jsonSuccess() {
        return renderJson(null);
    }

    /**
     * json格式失败返回
     *
     * @return
     */
    protected String jsonFail() {
        return renderJson();
    }

    /**
     * json格式失败返回
     *
     * @param code
     * @return
     */
    protected String jsonFail(int code) {
        return renderJson(code);
    }

    /**
     * json格式失败返回
     *
     * @param code
     * @param message
     * @return
     */
    protected String jsonFail(int code, String message) {
        return renderJson(code, message);
    }

    /**
     * json格式失败返回
     *
     * @param constants
     * @return
     */
    protected String jsonFail(BusinessConstants constants) {
        return renderJson(constants.getCode(), constants.getMsg());
    }

    /**
     * 渲染json数据
     *
     * @param data
     * @return
     */
    private <T> String renderJson(T data) {
        return ProtocolUtils.populateResponseJson(data);
    }

    /**
     * 渲染json数据
     *
     * @param data
     * @param message
     * @return
     */
    private <T> String renderJson(T data, String message) {
        if (data != null)
            return ProtocolUtils.populateResponseJson(message, data);
        return renderJson();
    }

    /**
     * 渲染json数据
     *
     * @param data
     * @param message
     * @return
     */
    private <T> String renderJsonForNull(T data, String message) {
        if (data != null)
            return ProtocolUtils.populateResponseJsonForNull(message, data);
        return renderJson();
    }

    /**
     * 渲染json数据
     *
     * @param code
     * @param message
     * @return
     */
    private String renderJson(int code, String message) {
        return ProtocolUtils.populateResponseJson(code, message);
    }

    /**
     * 渲染json数据
     *
     * @param code
     * @return
     */
    private String renderJson(int code) {
        return ProtocolUtils.populateResponseJson(code);
    }

    /**
     * 渲染json数据
     *
     * @return
     */
    private String renderJson() {
        return ProtocolUtils.populateResponseJson();
    }

    /**
     * 检查用户是否登录，如果登录返回userId,否则异常退出
     *
     * @param request
     * @return
     */
    protected long checkAndGetUserId(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
//        Long userId = 603L;
        if (userId == null)
            throw new BusinessException(BusinessConstants.USER_NOT_LOGIN.getCode(), "用户未登录");
        return userId.longValue();
    }

    /**
     * 检查用户是否登录，如果登录返回userId,否则异常退出
     *
     * @param request
     * @return
     */
    protected long getLoginUserId(HttpServletRequest request) {
        Object uid = request.getAttribute("userId");
        if (uid == null) return 0L;
        Long userId = (Long) uid;
        return userId.longValue();
    }

    /**
     * 获取平台信息
     *
     * @return {@link LogPlatformDto} 设备信息
     */
    protected LogPlatformDto getLogPlatformDto(HttpServletRequest request) {
        return HeaderUtil.getHeaderClientInfo(request);
    }

    /**
     * 检查用户是否登录，如果登录返回userId,否则异常退出
     *
     * @param request
     * @return
     */
    protected String checkAndGetUserAccount(HttpServletRequest request) {
        String account = (String) request.getAttribute("account");
        if (StringUtils.isBlank(account))
            throw new BusinessException(BusinessConstants.USER_NOT_LOGIN.getCode(), "用户未登录");
        return account;
    }

    /**
     * 检查用户是否登录，如果登录返回ssn,否则异常退出
     *
     * @param request
     * @return
     */
    protected String checkAndGetUserSsn(HttpServletRequest request) {
        String ssn = (String) request.getAttribute("ssn");
        if (StringUtils.isBlank(ssn))
            throw new BusinessException(BusinessConstants.USER_NOT_LOGIN.getCode(), "用户未登录");
        return ssn;
    }

    protected long checkAndGetMcnId(HttpServletRequest request) {
        String userType = (String) request.getAttribute("user-type");
        Long mcnId = null;
        if ("mcn".equals(userType)) {
            mcnId = (Long) request.getAttribute("userId");
        }
        if (mcnId == null) {
            throw new BusinessException(BusinessConstants.USER_NOT_LOGIN.getCode(), "用户未登录");
        }
        return mcnId;
    }

    /**
     * 检查用户是否登录，如果登录返回userId, 未登录，则返回null的userId
     *
     * @param request
     * @return
     */
    protected Long getUserId(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
//        Long userId = 372L;
        return userId;
    }

    protected String getUserOsName(HttpServletRequest request) {
        String osName = (String) request.getAttribute("osName");
        return osName;
    }

    protected static boolean isWeixin(HttpServletRequest request) {
        String ua = request.getHeader("user-agent");
        if (StringUtils.isBlank(ua)) {
            return false;
        }

        if (ua.contains("MicroMessenger")) {
            return true;
        }

        return false;
    }

    protected String checkAndGetUsername(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String username = (String) session.getAttribute("userName");
        if (username == null) {
            throw new BusinessException(BusinessConstants.USER_NOT_LOGIN.getCode(), "用户未登录");
        }
        return username;
    }
}
